package com.java.myapp;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;

public class Pregis extends JFrame {
	
	private JTextField txtUsername;
	private JPasswordField txtPassword;
	private JPasswordField txtConfirmPassword;
	private JTextField txtAge;
	private JTextField txtWeight;
	private JTextField txtHigh;

	/**
	 * Launch the application.
	 */


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				Pregis frame = new Pregis();
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Pregis() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 454, 343);
		setTitle("CalMe");
		getContentPane().setLayout(null);

		// Header Title
		JLabel hRegister = new JLabel("Register Data");
		hRegister.setBounds(121, 11, 132, 20);
		hRegister.setFont(new Font("Tahoma", Font.BOLD, 13));
		hRegister.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(hRegister);

		// *** Header ***//
		JLabel hUsername = new JLabel("Username :");
		hUsername.setBounds(78, 52, 89, 14);
		getContentPane().add(hUsername);
		
		JLabel hPassword = new JLabel("Password :");
		hPassword.setBounds(78, 84, 89, 14);
		getContentPane().add(hPassword);
		
		JLabel hConfirmPassword = new JLabel("Confirm Password :");
		hConfirmPassword.setBounds(77, 113, 130, 14);
		getContentPane().add(hConfirmPassword);
		
		JLabel hAge = new JLabel("Age :");
		hAge.setBounds(78, 148, 89, 14);
		getContentPane().add(hAge);
		
		JLabel lblGender = new JLabel("Gender :");
		lblGender.setBounds(246, 148, 57, 14);
		getContentPane().add(lblGender);
		
		JRadioButton rdbtnMale = new JRadioButton("Male");
		rdbtnMale.setBounds(294, 144, 52, 23);
		getContentPane().add(rdbtnMale);
		
		JRadioButton rdbtnFemale = new JRadioButton("Female");
		rdbtnFemale.setBounds(348, 144, 69, 23);
		getContentPane().add(rdbtnFemale);
		

		JLabel hWeight = new JLabel("Weight :");
		hWeight.setBounds(78, 176, 89, 14);
		getContentPane().add(hWeight);
		
		JLabel hHigh = new JLabel("High :");
		hHigh.setBounds(246, 176, 46, 14);
		getContentPane().add(hHigh);
		
		



		// CustomerID
		txtUsername = new JTextField("");
		txtUsername.setBounds(217, 47, 99, 20);
		getContentPane().add(txtUsername);
		
		// Password
		txtPassword = new JPasswordField();
		txtPassword.setBounds(217, 77, 102, 20);
		getContentPane().add(txtPassword);
		
		// Confirm Password
		txtConfirmPassword = new JPasswordField();
		txtConfirmPassword.setBounds(217, 112, 102, 20);
		getContentPane().add(txtConfirmPassword);

		// age
		txtAge = new JTextField();
		txtAge.setBounds(138, 145, 89, 20);
		getContentPane().add(txtAge);

		// weight
		txtWeight = new JTextField();
		txtWeight.setBounds(138, 173, 89, 20);
		getContentPane().add(txtWeight);
		
		// high
		txtHigh = new JTextField();
		txtHigh.setBounds(284, 173, 86, 20);
		getContentPane().add(txtHigh);
		//txtHigh.setColumns(10);

		
		// Save Button
		JButton btnSave = new JButton("Save");
		btnSave.setBounds(161, 227, 89, 23);
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(RegisterData()) {
					JOptionPane.showMessageDialog(null,
							"Register Data Successfully");
				}
			}
		});
		getContentPane().add(btnSave);		
		
		
		
		
	}
	
	private Boolean RegisterData()
	{
		
		String strUsername = txtUsername.getText();
		String strPassword = new String(txtPassword.getPassword());
		String strConfirmPassword = new String(txtConfirmPassword.getPassword());
		String strAge = txtAge.getText();
		String strWeight = txtWeight.getText();
		String strHigh = txtHigh.getText();
		
		if(strUsername.equals("")) // Username
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (Username)");
			txtUsername.requestFocusInWindow(); 
			return false;
		}
		if(strPassword.equals("")) // Password
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (Password)");
			txtPassword.requestFocusInWindow(); 
			return false;
		}
		
		if(strConfirmPassword.equals("")) // Confirm Password
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (Confirm Password)");
			txtConfirmPassword.requestFocusInWindow(); 
			return false;
		}
		if(!strPassword.equals(strConfirmPassword)) // Password math
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (Password Not Match!)");
			txtPassword.requestFocusInWindow(); 
			return false;
		}		
		if(strAge.equals("")) // Age
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (Age)");
			txtAge.requestFocusInWindow(); 
			return false;
		}	
		
		if(strWeight.equals("")) // Weight
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (Weight)");
			txtWeight.requestFocusInWindow(); 
			return false;
		}	
		
		if(strHigh.equals("")) //High
		{
			JOptionPane.showMessageDialog(null,
					"Please Input (High)");
			txtHigh.requestFocusInWindow(); 
			return false;
		}	
		
		Connection connect = null;
		Statement s = null;
		Boolean status = false;

		try {
			Class.forName("com.mysql.jdbc.Driver"); 
			
			connect = DriverManager.getConnection(""+ "jdbc:mysql://localhost/mydatabase"+ "?user=root&password=root"); 

			s = connect.createStatement();
			
			// SQL Insert
			String sql = "INSERT INTO member "
					+ "(Username,Password,Age,Weight,High) "
					+ "VALUES ('" + strUsername + "','"
					+ strPassword + "','"
					+ strAge + "'" + ",'"
					+ strWeight + "','"
					+ strHigh + "') ";
			s.execute(sql);
		
			
			// Reset Text Fields
			txtUsername.setText("");
			txtPassword.setText("");
			txtConfirmPassword.setText("");
			txtAge.setText("");
			txtWeight.setText("");
			txtHigh.setText("");
				
			status  = true;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}

		try {
			if (s != null) {
				s.close();
				connect.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return status;

	}
	}


